﻿using Microsoft.SharePoint.Administration;
using SPSDeploy.Extensions.Controllers;
using System.Xml.Linq;

namespace SPSDeploy.Extensions.Modules.Extract
{
    /// <summary>
    /// Service application extract module
    /// </summary>
    internal class ServiceApplicationModule : BaseModule
    {

        /// <summary>
        /// Initialization
        /// </summary>
        /// <param name="controller"></param>
        internal ServiceApplicationModule(BaseController controller)
            : base(controller)
        {
        }

        /// <summary>
        /// Execute module
        /// </summary>
        /// <param name="parent"></param>
        /// <returns></returns>
        internal override XElement Execute(XElement parent)
        {
            SPFarm farm = SPFarm.Local;
            XElement result = new XElement("serviceApplications");
            foreach (SPService service in farm.Services)
                foreach (SPServiceApplication application in service.Applications)
                    result.Add(new XElement("serviceApplication",
                        new XAttribute("name", application.DisplayName),
                        new XAttribute("type", application.TypeName)));
            parent.Add(result);
            controller.OnProgressMessage("Service application information extracted.");
            return result;
        }

    }
}